﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;
using Telerik.Web.UI.Upload;
using System.IO;
using Telerik.Web.UI;


namespace MediaMonitor.Admin
{
    public partial class Media_Mgmt_AddEdit : Adex.BasePage
    {


        int MaxFileSizeInMB = Utility.ToInt(ConfigurationManager.AppSettings["MaxVideoFileSizeInMB"]);

        protected void Page_Load(object sender, EventArgs e)
        {


        }



        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (lstVersion.SelectedValue != "")
            {
                ProdVersion pv = new ProdVersion(lstVersion.SelectedValue);


                if (!(lstVersion.SelectedValue.Length > 0))
                {
                    lblMsg.Text = "You need to select a product version first.";
                    return;
                }

                string msg1 = "No full sized video file selected to upload";
                string msg2 = "No preview video file selected to upload";

                //upload full size video
                try
                {
                    if (flVideo.PostedFile.FileName.Length > 0)
                    {
                        string fName = flVideo.FileName;
                        string ext = fName.Substring(fName.LastIndexOf("."));
                        string newFilename = lstVersion.SelectedValue + "_" + Utility.GetUniqueCode();

                        if (Directory.Exists(Server.MapPath("~/videos/" + lstProduct.SelectedValue)) == false)
                            System.IO.Directory.CreateDirectory(Server.MapPath("~/videos/" + lstProduct.SelectedValue));

                        string file2Save = Server.MapPath("~/videos/" + lstProduct.SelectedValue + "/") + newFilename + ext;
                        flVideo.PostedFile.SaveAs(file2Save);
                        pv.FullVideo = true;
                        pv.FullVideoFile = newFilename + ext;
                        pv.DateModified = DateTime.Now;
                        msg1 = "Full sized video uploaded successfully";

                    }

                }
                catch (Exception ex)
                {
                    msg1 = "System failed to upload the full sized vidoe file [" + ex.Message + "]";
                }

                //upload preview video
                try
                {
                    if (flVideo_Short.PostedFile.FileName.Length > 0)
                    {
                        string fName = flVideo_Short.FileName;
                        string ext = fName.Substring(fName.LastIndexOf("."));
                        string newFilename = lstVersion.SelectedValue + "_s";

                        if (Directory.Exists(Server.MapPath("~/videos/" + lstProduct.SelectedValue)) == false)
                            System.IO.Directory.CreateDirectory(Server.MapPath("~/videos/" + lstProduct.SelectedValue));

                        string file2Save = Server.MapPath("~/videos/" + lstProduct.SelectedValue + "/") + newFilename + ext;
                        flVideo_Short.SaveAs(file2Save);

                        pv.ShortVideo = true;
                        pv.ShortVideoFile = newFilename + ext;
                        pv.DateModified = DateTime.Now;
                        msg2 = "Preview video file uploaded successfully";
                    }
                }
                catch (Exception ex)
                {
                    msg2 = "System failed to upload the preview vidoe file [" + ex.Message + "]";
                }


                pv.Save(); //save ProdVersion
                lblMsg.Text = "<ul><li>" + msg1 + "</li><li>" + msg2 + "</li></ul>";
            }
            else
            {
                lblMsg.Text = "You must select a valid Product Version first.";
                return;

            }
        }


        protected void lstAdvertiser_SelectedIndexChanged(object o, EventArgs e)
        {
            lstBrand.DataBind();
            lstBrand.ClearSelection();
            lstBrand.SelectedIndex = 0;
            lstBrand_SelectedIndexChanged(lstBrand, new EventArgs());
        }
        protected void lstBrand_SelectedIndexChanged(object o, EventArgs e)
        {
            lstProduct.DataBind();
            lstProduct.ClearSelection();
            lstBrand.SelectedIndex = 0;
            lstProduct_SelectedIndexChanged(lstProduct, new EventArgs());
        }
        protected void lstProduct_SelectedIndexChanged(object o, EventArgs e)
        {
            lstVersion.DataBind();
            lstVersion.ClearSelection();
            lstVersion.SelectedIndex = 0;
            lstVersion_SelectedIndexChanged(lstVersion, new EventArgs());

        }
        protected void lstVersion_SelectedIndexChanged(object o, EventArgs e)
        {
            btnUpload.Visible = true;
        }



    }
}